package com.yt.common.constant;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yt.common.bo.HeadParamsBO;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CommonParameter {

    // 创建线程局部变量，并初始化值
    private static ThreadLocal<HeadParamsBO> memberIdThreadLocal = new ThreadLocal<HeadParamsBO>() {
        protected HeadParamsBO initialValue() {
            return new HeadParamsBO();
        }
    };

    // 提供线程局部变量set方法
    public static void setHeadParamsBO(HeadParamsBO bo) {
        memberIdThreadLocal.set(bo);
    }

    // 提供线程局部变量get方法
    public static HeadParamsBO getHeadParamsBO() {
        return memberIdThreadLocal.get();
    }

    public static void plintInfo(){
        JSONObject json = JSON.parseObject(CommonParameter.getHeadParamsBO().getBodyParams());
        long startTime = (Long) json.get("startTime") == null ? 0 : (Long) json.get("startTime");
        long endTime = System.currentTimeMillis();
        long consumeTime = endTime - startTime;//消耗时间
        json.put("endTime", endTime);
        json.put("consumeTime", consumeTime);
//        log.info("client params：" + JSON.toJSONString(CommonParameter.getHeadParamsBO()));
    }

    public static void main(String[] args) {
        String str = "{\"ip:\":\"127.0.0.1\",\"methodName\":\"com.cq.user.api.client.UserLoginApiController.getVerify\",\"startTime\":1701747718837,\"params\":\"{\\\"randomStr\\\":[\\\"6805\\\"],\\\"signature\\\":[\\\"917d4802cc48284856c68f41df8c87ac\\\"]}\",\"url:\":\"/user-client/api/userLogin/getVerify\"}";
        JSONObject js = JSON.parseObject(str);
        System.out.println("---ip->" + js.get("ip"));
    }

}
